package com.kaisa.activity.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.kaisa.activity.R;
import com.kaisa.activity.adapter.EnjoyHolidayAdapter;
import com.kaisa.activity.bean.EnjoyHoliday;
import com.kaisa.activity.constant.UrlHttp;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class EnjoyHolidayActivity extends AppCompatActivity implements View.OnClickListener{

    @Bind(R.id.top_back)
    ImageView topBack;
    @Bind(R.id.btn_search)
    Button btnSearch;
    @Bind(R.id.holiday_lv)
    ListView holidayLv;
    private EnjoyHolidayAdapter adapter;
    private List<EnjoyHoliday.DataEntity> list = new ArrayList<EnjoyHoliday.DataEntity>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enjoy_holiday);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initData() {
        topBack.setOnClickListener(this);
        x.http().get(new RequestParams(UrlHttp.ENJOY_HOLIDAY_URL), new Callback.CacheCallback<String>() {
            @Override
            public void onSuccess(String s) {
                EnjoyHoliday entity = JSON.parseObject(s,EnjoyHoliday.class);
                list.addAll(entity.getData());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(String s) {
                return false;
            }
        });
    }

    private void initView() {
        adapter = new EnjoyHolidayAdapter(list,this);
        holidayLv.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.top_back:
                this.finish();
                break;
        }
    }
}
